<?php


namespace App\Repositories\Sociaty;


use App\Models\MainDB\UserBank;
use App\Models\MainDB\UserBankinfo;
use App\Models\SociatyDB\SociatySettle;
use App\Traits\Singleton;
use Carbon\Carbon;

class SettleRepository
{
    use Singleton;

    public function getLists($sociatyId,$year)
    {

        $lists= SociatySettle::where(['sociaty_id'=>$sociatyId,'year'=>$year])
                ->orderBy('created_at','desc')
                ->get();

        return $lists;
    }

    public function detail($settleId)
    {
        $detail= SociatySettle::where(['id'=>$settleId])
                ->first();
        return $detail;
    }

    public function getBankInfo($userId)
    {
        $detail= UserBankinfo::where(['user_id'=>$userId])
            ->first();
        return $detail?:[];
    }

    public function newGetBankInfo($userId)
    {
        $detail = UserBank::query()->where(['user_id' => $userId, 'bank_type' => 3])->first();

        $data = [
            'id'          => $detail['id'],
            'user_id'     => $detail['user_id'],
            'bank_name'   => $detail['bank_name'],
            'bank_number' => $detail['bank_num'],
            'realname'    => $detail['cardholder'],
            'idcard'      => $detail['card_number'],
            'bank_phone'  => $detail['mobile'],
            'created_at'  => '',
            'updated_at'  => '',
        ];
        return !empty($detail) ? $data : null;

    }

    public function setBankInfo($userId,$params)
    {
        $bankModel=UserBankinfo::where(['user_id'=>$userId])->first();
        if(!$bankModel){
            $res=UserBankinfo::create(array_merge(['user_id'=>$userId],$params))->save();
        }else{
            $res= $bankModel->update($params);
        }

        return $res;
    }

    public function newSetBankInfo($userId, $params)
    {
        $backModel = UserBank::query()->where(['user_id' => $userId,'bank_type'=>3])->first();

        $data['bank_zhi']    = $params['bank_zhi'];
        $data['card_number'] = $params['idcard'];
        $data['bank_name']   = $params['bank_name'];
        $data['bank_num']    = $params['bank_number'];
        $data['cardholder']  = $params['realname'];
        $data['mobile']      = $params['bank_phone'];
        $data['user_id']     = $userId;
        if (!$backModel) {
            $data['bank_type'] = 3;
            $data['add_time']  = Carbon::now()->timestamp;
            $res               = UserBank::query()->insert($data);
        } else {
            $res = $backModel->update($data);

        }
        return $res;
    }


    public function repairBank()
    {
        $bank = UserBankinfo::query()->get();
        $all  = [];
        foreach ($bank as $value) {
            $data                = [];
            $data['bank_zhi']    = $value['bank_zhi'];
            $data['card_number'] = $value['idcard'];
            $data['bank_name']   = $value['bank_name'];
            $data['bank_num']    = $value['bank_number'];
            $data['cardholder']  = $value['realname'];
            $data['mobile']      = $value['bank_phone'];
            $data['user_id']     = $value['user_id'];
            $data['bank_type'] = 3;
            $data['add_time']  = Carbon::now()->timestamp;
            $all[]               = $data;
        }

        UserBank::query()->insert($all);

    }
    
}